package angel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.util.Log4jConfigurer;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class FreeMarkerEngine {

	private final static Properties KEYWORDS = new Properties();
//	static{
//		try{
//			if(KEYWORDS.isEmpty()){
//				KEYWORDS.load(Shrinksafe.class.getResourceAsStream("ShrinksafeKeywords.properties"));
//			}					
//		}
//		catch(IOException e){
//			//logger.error("Failed to load ShrinksafeKeywords.properties",e);
//		}
//	}
	
	public static void main(String[] args) throws Exception{
//		Log4jConfigurer.initLogging("E:/development/workspace-kepler/Angel/WebContent/WEB-INF/log4j.properties");
		Logger logger = LoggerFactory.getLogger(FreeMarkerEngine.class);
		
		Configuration cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File("/home/xwp/cngit/beyond-prototype/src/main/resources/templates/"));
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		cfg.setSharedVariable("CompilationLevel", "SIMPLE_OPTIMIZATIONS");
//		cfg.setSharedVariable("replaceKeyword", "false");
//		cfg.setSharedVariable("PrettyPrint", "true");
		
		Map modelMap = new HashMap();				
		modelMap.put("context", "");			
		modelMap.put("project", "prototype10");
//		modelMap.put("webAppContext", "");
//		modelMap.put("webAppRootKey", "angel");
//		modelMap.put("webAppVersion", "0.2.3");		
		modelMap.put("host", "http://beyond-prototype.xyz");
		modelMap.put("webAppHost", "");
//		modelMap.put("webAppProjects", "prototype10");
//		modelMap.put("bootstrapMIME", "js");	
//		modelMap.put("indexMIME", "html");
		modelMap.put("shrink", true);	
		modelMap.put("shrinkCached", false);
		modelMap.put("hasServerResource", false);		
		modelMap.put("applicationMode", "PROD");
		
		java.util.Random r = new java.util.Random();		
		//modelMap.put("seed",String.valueOf(r.nextInt(2^10)*r.nextInt(2^10)));
		//modelMap.put("seed",String.valueOf(System.currentTimeMillis()*r.nextGaussian()));
		modelMap.put("seed","12345678901234567890".substring(r.nextInt(18)));
		String[] dest = new String[]{"/home/xwp/cngit/beyond-prototype/src/main/resources/public/"};
		
		Map<String,String> sources = new HashMap();
//		sources.put("index.ftl", "index.html");
//		sources.put("indexCss.ftl", "index.css");
//		sources.put("indexCacheManifest.ftl", "indexCacheManifest.manifest");
		
		sources.put("index1.ftl", "index.html");
		sources.put("index1CacheManifest.ftl", "index1CacheManifest.manifest");
		sources.put("index1Shadow.ftl", "index1Shadow.html");
		
		
		sources.put("prototype10/index.ftl", "prototype10/index.html");
		sources.put("prototype10/indexCacheManifest.ftl", "prototype10/indexCacheManifest.manifest");
		
		sources.put("prototype10/index1.ftl", "prototype10/index1.html");
		sources.put("prototype10/index1Shadow.ftl", "prototype10/index1Shadow.html");		
		sources.put("prototype10/index1CacheManifest.ftl", "prototype10/index1CacheManifest.manifest");
		
		sources.put("prototype10/require?indexJs", "prototype10/index.js");
		sources.put("prototype10/require?indexCss", "prototype10/index.css");
		sources.put("prototype10/require?bootstrap", "prototype10/bootstrap.js");
		
		sources.put("prototype10/preview.ftl", "prototype10/preview.html");
		
		Iterator<String> files = sources.keySet().iterator();
		while(files.hasNext()){
			Template tpl1 = null;
			String tpl = files.next();
			logger.info("Start parsing {}",tpl);
			if(tpl.startsWith("prototype10/require?")){
				//String widget = tpl.replace("prototype/agile", "agile");
				String widget = tpl.replace("prototype10/require?", "");
				modelMap.put("widget",widget);
				modelMap.put("widget_abbr", KEYWORDS.getProperty(widget, widget));
				tpl1 = cfg.getTemplate("prototype10/require.ftl");
			}else{
				tpl1 = cfg.getTemplate(tpl);
			}

			Writer writer = null;
			int i = 0;File target;
//			for(i = 0; i < dest.length; i++){
				try{
					target = new File(dest[i] + sources.get(tpl));
					if(!target.exists()){
						target.getParentFile().mkdirs();
					}
					writer = new OutputStreamWriter(new FileOutputStream(target));
					tpl1.process(modelMap, writer);
					writer.flush();		
				}
				finally{
					if(writer != null){
						writer.close();
					}
				}	
//			}
				
			logger.info("Complete parsing {}",tpl);
			for(i = 1; i < dest.length; i++){
				//FileUtils.copyFileToDirectory(target, new File(dest[i]));
				FileUtils.copyFile(target, new File(dest[i] + sources.get(tpl)));
			}
		}
		
//		FileUtils.touch(new File("G:/development/workspace-kepler/Angel/WebContent/appCache.manifest"));
//		FileUtils.touch(new File("G:/development/workspace-kepler/Angel/WebContent/prototype/appCache.manifest"));
//		FileUtils.copyFile(new File("G:/development/workspace-kepler/Angel/WebContent/appCache.manifest"), new File("G:/home/jmv520/appCache.manifest"));	
//		FileUtils.copyFile(new File("G:/development/workspace-kepler/Angel/WebContent/prototype/appCache.manifest"), new File("G:/home/jmv520/prototype/appCache.manifest"));
//		FileUtils.writeStringToFile(null, "", true);
		
//		Log4jConfigurer.shutdownLogging();
		
		System.exit(0);
	}
}
